{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "device count: 14\n",
      "index: 0, name: HDA NVidia: HDMI 0 (hw:0,3), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 2\n",
      "index: 1, name: HDA NVidia: HDMI 1 (hw:0,7), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 2, name: HDA NVidia: HDMI 2 (hw:0,8), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 3, name: HDA NVidia: HDMI 3 (hw:0,9), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 4, name: USB Speaker-Mic: Audio (hw:1,0), 采样率: 48000.0 Hz, 输入通道: 1, 输出通道: 2\n",
      "index: 5, name: HD-Audio Generic: HDMI 0 (hw:2,3), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 6, name: HD-Audio Generic: HDMI 1 (hw:2,7), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 7, name: HD-Audio Generic: HDMI 2 (hw:2,8), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 8, name: HD-Audio Generic: HDMI 3 (hw:2,9), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 8\n",
      "index: 9, name: HD-Audio Generic: ALC897 Analog (hw:3,0), 采样率: 44100.0 Hz, 输入通道: 2, 输出通道: 2\n",
      "index: 10, name: HD-Audio Generic: ALC897 Digital (hw:3,1), 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 2\n",
      "index: 11, name: hdmi, 采样率: 44100.0 Hz, 输入通道: 0, 输出通道: 2\n",
      "index: 12, name: pulse, 采样率: 44100.0 Hz, 输入通道: 32, 输出通道: 32\n",
      "index: 13, name: default, 采样率: 44100.0 Hz, 输入通道: 32, 输出通道: 32\n"
     ]
    }
   ],
   "source": [
    "import pyaudio\n",
    "\n",
    "pa = pyaudio.PyAudio()\n",
    "dev_count = pa.get_device_count()\n",
    "print(f\"device count: {dev_count}\")\n",
    "\n",
    "for i in range(dev_count):\n",
    "    dev_info = pa.get_device_info_by_index(i)\n",
    "    print(\n",
    "        f\"index: {dev_info['index']}, \"\n",
    "        f\"name: {dev_info['name']}, \"\n",
    "        f\"采样率: {dev_info['defaultSampleRate']} Hz, \"\n",
    "        f\"输入通道: {dev_info['maxInputChannels']}, \"\n",
    "        f\"输出通道: {dev_info['maxOutputChannels']}\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "find input device: 4, name: USB Speaker-Mic: Audio (hw:1,0), 采样率: 48000.0 Hz, 输入通道: 1\n",
      "find output device: 4, name: USB Speaker-Mic: Audio (hw:1,0), 采样率: 48000.0 Hz, 输出通道: 2\n"
     ]
    }
   ],
   "source": [
    "target_dev_name = \"USB Speaker-Mic\"\n",
    "\n",
    "for i in range(dev_count):\n",
    "    dev_info = pa.get_device_info_by_index(i)\n",
    "    if dev_info[\"maxInputChannels\"] > 0 and target_dev_name in dev_info[\"name\"]:\n",
    "        print(f\"find input device: {dev_info['index']}, name: {dev_info['name']}, 采样率: {dev_info['defaultSampleRate']} Hz, 输入通道: {dev_info['maxInputChannels']}\")\n",
    "\n",
    "    if dev_info[\"maxOutputChannels\"] > 0 and target_dev_name in dev_info[\"name\"]:\n",
    "        print(f\"find output device: {dev_info['index']}, name: {dev_info['name']}, 采样率: {dev_info['defaultSampleRate']} Hz, 输出通道: {dev_info['maxOutputChannels']}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pyaudio 回调函数中的 pyaudio.paContinue vs pyaudio.paComplete\n",
    "| 特性 | paContinue | paComplete |\n",
    "|:-------:|:--------:|:-------:|\n",
    "|流的生命周期|流保持活动状态|流自动停止|\n",
    "| 回调是否继续被调用   | 是    | 否   |\n",
    "| 典型用例   | 实时音频输入 / 输出    | 播放固定长度音频文件   |\n",
    "| 后续操作   | 需要手动调用 stop_stream()    | 流自动停止，可能需要关闭资源   |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatbot_temp",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
